package vboot.core.module.ass.dict.data;

import vboot.core.common.api.R;
import vboot.core.common.dao.JdbcDao;
import vboot.core.common.dao.Sqler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vboot.core.xutil.lang.XstrUtil;


@RestController
@RequestMapping("ass/dict/data")
public class AssDictDataApi {

    private String table = "ass_dict_data";

    @GetMapping
    public R get(String name, String maiid) {
        Sqler sqler = new Sqler(table);
        if(XstrUtil.isNotBlank(name)){
            sqler.addWhere("(t.name like ? or t.id like ?)","%"+name+"%","%"+name+"%");
        }
        sqler.addEqual("t.maiid", maiid);
        sqler.addSelect("t.notes,t.ornum,t.code");
        sqler.addOrder("t.ornum");
        return R.ok(service.findPageData(sqler));
    }

    @GetMapping("list")
    public R getList(String maiid) {
        Sqler sqler = new Sqler(table);
        sqler.addEqual("t.maiid",maiid);
        sqler.addOrder("t.ornum");
        return R.ok( jdbcDao.findIdNameList(sqler));
    }

    @GetMapping("one/{id}")
    public R getOne(@PathVariable String id) {
        AssDictData main = service.findOne(id);
        return R.ok(main);
    }

    @PostMapping
    public R post(@RequestBody AssDictData main) {
        return R.ok(service.insert(main));
    }

    @PutMapping
    public R put(@RequestBody AssDictData main) {
        return R.ok(service.update(main));
    }

    @DeleteMapping("{ids}")
    public R delete(@PathVariable String[] ids) {
        return R.ok(service.delete(ids));
    }

    @Autowired
    private AssDictDataService service;

    @Autowired
    private JdbcDao jdbcDao;

}
